// components/classifylabel/classifylabel.js
Component({
  // 生命周期函数
  lifetimes: {
    created() {
      let that = this;
      // 获取地址列表
      wx.request({
        url: 'http://api_devs.wanxikeji.cn/api/userAddressList?token=3c35c3d0738a879f23926f4676b2874e',
        success(res) {
          let add1 = [];
          res.data.data.forEach((element, index) => {
            let add = [];
            add.push(element.procince)
            add.push(element.city)
            add.push(element.area)
            let string = add.join(' ')
            if (index == 0) {
              add1.push({ address: string, status: true })
            } else {
              add1.push({ address: string, status: false })
            }
          });
          that.setData({ addresses: add1 });
          that.setData({ address: that.data.addresses[0].address });
        },
        method: "POST"
      });
    }
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    sourcedata: [
      {
        id: "0",
        title: [],
        tt: "品牌",
        child: [
          { title: "农夫山泉", status: false },
          { title: "怡宝", status: false },
          { title: "景田", status: false },
          { title: "恒大冰泉", status: false },
          { title: "屈臣氏", status: false },
          { title: "百岁山", status: false },
          { title: "昆仑山", status: false },
          { title: "康师傅", status: false },
          { title: "雀巢优活", status: false },
          { title: "泉阳泉", status: false },
          { title: "崂山", status: false },
          { title: "阿尔卑斯", status: false },
          { title: "天地精华", status: false },
          { title: "5100", status: false },
          { title: "白山水", status: false },
          { title: "润田翠", status: false },
          { title: "舒达源", status: false },
          { title: "统一", status: false },
          { title: "水易方", status: false },
          { title: "倍特", status: false },
          { title: "火山鸣泉", status: false },
          { title: "今麦郎", status: false },
          { title: "巴马丽琅", status: false },
          { title: "芙丝", status: false },
          { title: "大秦之水", status: false },
        ]
      },
      {
        id: "1",
        title: [],
        tt: "单件容量",
        child: [
          { title: "400ml以下", status: false },
          { title: "401-600ml", status: false },
          { title: "601-999ml", status: false },
          { title: "1-2L", status: false },
          { title: "2L以上", status: false },
        ]
      },
      {
        id: "2",
        title: [],
        tt: "净含量",
        child: [
          { title: "400ml以下", status: false },
          { title: "401-600ml", status: false },
          { title: "601-999ml", status: false },
          { title: "1-2L", status: false },
          { title: "2L以上", status: false },
          { title: "501g-1kg", status: false },
        ]
      },
      {
        id: "3",
        title: [],
        tt: "类别",
        child: [
          { title: "母婴水", status: false },
          { title: "纯净水", status: false },
          { title: "矿物质水", status: false },
          { title: "饮用天然水", status: false },
          { title: "苏打水", status: false },
          { title: "气泡水", status: false },
          { title: "水卡/水票", status: false },
        ]
      },
      {
        id: "4",
        title: [],
        tt: "包装形式",
        child: [
          { title: "桶装", status: false },
          { title: "罐装", status: false },
          { title: "瓶装", status: false },
          { title: "塑膜包装", status: false },
          { title: "礼盒装", status: false },
          { title: "盒装", status: false },
          { title: "箱装", status: false },
        ]
      },
      {
        id: "5",
        title: [],
        tt: "包装规格",
        child: [
          { title: "1-6", status: false },
          { title: "7-12", status: false },
          { title: "13-24", status: false },
          { title: "24以上", status: false },
        ]
      },
      {
        id: "6",
        title: [],
        tt: "产地",
        child: [
          { title: "澳大利亚", status: false },
          { title: "港澳台地区", status: false },
          { title: "德国", status: false },
          { title: "英国", status: false },
          { title: "法国", status: false },
          { title: "美国", status: false },
          { title: "加拿大", status: false },
          { title: "马来西亚", status: false },
          { title: "查看全部", status: false },
        ]
      },
      {
        id: "7",
        title: [],
        tt: "口味",
        child: [
          { title: "原味", status: false },
          { title: "柠檬味", status: false },
          { title: "西柚味", status: false },
          { title: "桃子味", status: false },
          { title: "青柠味", status: false },
          { title: "香草味", status: false },
          { title: "混合口味", status: false },
        ]
      },
    ],
    sourcedata1: [
      {
        id: "0",
        title: [],
        tt: "品牌",
        child: [
          { title: "农夫山泉", status: false },
          { title: "怡宝", status: false },
          { title: "景田", status: false },
          { title: "恒大冰泉", status: false },
          { title: "屈臣氏", status: false },
          { title: "百岁山", status: false },
          { title: "昆仑山", status: false },
          { title: "康师傅", status: false },
          { title: "查看全部", status: false },
          { title: "雀巢优活", status: false },
          { title: "泉阳泉", status: false },
          { title: "崂山", status: false },
          { title: "阿尔卑斯", status: false },
          { title: "天地精华", status: false },
          { title: "5100", status: false },
          { title: "白山水", status: false },
          { title: "润田翠", status: false },
          { title: "舒达源", status: false },
          { title: "统一", status: false },
          { title: "水易方", status: false },
          { title: "倍特", status: false },
          { title: "火山鸣泉", status: false },
          { title: "今麦郎", status: false },
          { title: "巴马丽琅", status: false },
          { title: "芙丝", status: false },
          { title: "大秦之水", status: false },
        ]
      },
      {
        id: "1",
        title: [],
        tt: "单件容量",
        child: [
          { title: "400ml以下", status: false },
          { title: "401-600ml", status: false },
          { title: "601-999ml", status: false },
          { title: "1-2L", status: false },
          { title: "2L以上", status: false },
        ]
      },
      {
        id: "2",
        title: [],
        tt: "净含量",
        child: [
          { title: "400ml以下", status: false },
          { title: "401-600ml", status: false },
          { title: "601-999ml", status: false },
          { title: "1-2L", status: false },
          { title: "2L以上", status: false },
          { title: "501g-1kg", status: false },
        ]
      },
      {
        id: "3",
        title: [],
        tt: "类别",
        child: [
          { title: "母婴水", status: false },
          { title: "纯净水", status: false },
          { title: "矿物质水", status: false },
          { title: "饮用天然水", status: false },
          { title: "苏打水", status: false },
          { title: "气泡水", status: false },
          { title: "水卡/水票", status: false },
        ]
      },
      {
        id: "4",
        title: [],
        tt: "包装形式",
        child: [
          { title: "桶装", status: false },
          { title: "罐装", status: false },
          { title: "瓶装", status: false },
          { title: "塑膜包装", status: false },
          { title: "礼盒装", status: false },
          { title: "盒装", status: false },
          { title: "箱装", status: false },
        ]
      },
      {
        id: "5",
        title: [],
        tt: "包装规格",
        child: [
          { title: "1-6", status: false },
          { title: "7-12", status: false },
          { title: "13-24", status: false },
          { title: "24以上", status: false },
        ]
      },
      {
        id: "6",
        title: [],
        tt: "产地",
        child: [
          { title: "澳大利亚", status: false },
          { title: "港澳台地区", status: false },
          { title: "德国", status: false },
          { title: "英国", status: false },
          { title: "法国", status: false },
          { title: "美国", status: false },
          { title: "加拿大", status: false },
          { title: "马来西亚", status: false },
          { title: "查看全部", status: false },
        ]
      },
      {
        id: "7",
        title: [],
        tt: "口味",
        child: [
          { title: "原味", status: false },
          { title: "柠檬味", status: false },
          { title: "西柚味", status: false },
          { title: "桃子味", status: false },
          { title: "青柠味", status: false },
          { title: "香草味", status: false },
          { title: "其他", status: false },
        ]
      },
    ],
    transportdata: [
      { title: "京东物流", status: false },
      { title: "有货优先", status: true },
      { title: "货到付款", status: false },
      { title: "211限时达", status: false },
      { title: "京东国际", status: false },
      { title: "PLUS专享", status: false },
      { title: "促销商品", status: false },
    ],
    isdb: false,
    isappear: false,
    isshow: false,
    isadd: false,
    ispinpai: false,
    address: "",
    addresses: [],
    logistics: { title: "京东物流", status: false },
    num: "",
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 点击弹出详细筛选框
    filter: function () {
      this.setData({ isdb: !this.data.isdb })
      this.setData({ isappear: false });
    },
    disappear: function () {
      this.setData({ isdb: false })
      this.setData({ isshow: false })
      this.setData({ ispinpai: false })
    },
    disappear1: function () {
      this.setData({ isappear: false })
    },
    // 京东配送点击事件
    choose: function (e) {
      let n = e.target.dataset.index;
      this.setData({ ['transportdata[' + n + '].status']: !this.data.transportdata[n].status })
      this.setData({ 'logistics.status': this.data.transportdata[0].status });
    },
    // 竖向筛选点击事件
    choose1: function (e) {
      if(e.currentTarget.dataset.index == 8){
        this.setData({ispinpai:!this.data.ispinpai})
        this.setData({ num: e.currentTarget.dataset.id });
        this.data.sourcedata[this.data.num].child[e.currentTarget.dataset.index].status = !this.data.sourcedata[this.data.num].child[e.currentTarget.dataset.index].status;
        this.showall(e);
      }else{
        this.setData({ num: e.currentTarget.dataset.id });
        let arr = this.data.sourcedata[this.data.num].title;
        // 判断选中状态
        if(arr.includes(e.currentTarget.dataset.one)){
          arr.splice(arr.indexOf(e.currentTarget.dataset.one),1);
          this.setData({ ['sourcedata[' + this.data.num + '].title']: arr });
        }else{
          arr.push(e.currentTarget.dataset.one);
          this.setData({ ['sourcedata[' + this.data.num + '].title']: arr });
        }
        this.data.sourcedata[this.data.num].child[e.currentTarget.dataset.index].status = !this.data.sourcedata[this.data.num].child[e.currentTarget.dataset.index].status;
        this.data.sourcedata1[this.data.num].child[e.currentTarget.dataset.index].status =!this.data.sourcedata1[this.data.num].child[e.currentTarget.dataset.index].status;
        this.initData();
      }
    },
    // 查看全部
    showall:function(e){
      console.log(1);
    },
    // 返回
    close:function(){
      this.setData({ isadd: false });
    },
    close1:function(){
      this.setData({ isshow: false });
    },
    initData:function(){
      this.setData(this.data);
    },
    // 点击弹出添加其他地址
    addaddress:function(){
      this.setData({ isadd: true });
    },
    // 确认筛选信息
    affirm: function () {
      this.setData({ isdb: false });
    },
    affirm1: function () {
      this.setData({ isappear: false });
    },
    // 点击横向导航筛选弹出框
    sifting: function (e) {
      this.setData({ num: e.currentTarget.dataset.id });
      console.log(e.currentTarget.dataset.id);
      this.setData({ isappear: !this.data.isappear });
    },
    // 横向导航选择
    checkthis: function (e) {
      let arr = this.data.sourcedata[this.data.num].title;
      if(arr.includes(e.currentTarget.dataset.one)){
        arr.splice(arr.indexOf(e.currentTarget.dataset.one),1);
        this.setData({ ['sourcedata[' + this.data.num + '].title']: arr });
      }else{
        arr.push(e.currentTarget.dataset.one);
        this.setData({ ['sourcedata[' + this.data.num + '].title']: arr });
      }
      this.setData({ ['sourcedata[' + this.data.num + '].child[' + e.currentTarget.dataset.index + '].status']: !this.data.sourcedata[this.data.num].child[e.currentTarget.dataset.index].status })
      this.setData({ ['sourcedata1[' + this.data.num + '].child[' + e.currentTarget.dataset.index + '].status']: !this.data.sourcedata1[this.data.num].child[e.currentTarget.dataset.index].status })
    },
    cancel:function(){
      let originaldata1 = this.data.sourcedata1[this.data.num].child;
      originaldata1.forEach(item => {
          item.status = false;
      });
      this.setData({ sourcedata1: originaldata1 });
      this.setData({ispinpai:false});
    },
    affirm2:function(){
      this.setData({ispinpai:false});
    },
    // 重置
    reset: function () {
      let originaldata = this.data.sourcedata;
      let originaldata1 = this.data.sourcedata1;
      originaldata.forEach(element => {
        element.title = [];
        element.child.forEach(item => {
          item.status = false;
        });
      });
      originaldata1.forEach(element => {
        element.title = [];
        element.child.forEach(item => {
          item.status = false;
        });
      });
      this.setData({ sourcedata: originaldata });
      this.setData({ sourcedata1: originaldata1 });
    },
    // 点击弹出地址框
    chooseaddress: function () {
      this.setData({ isshow: true });
    },
    // 选择地址单选按钮
    radiobtn: function (e) {
      this.setData({ ['addresses.[' + e.currentTarget.dataset.idx + '].status']: !this.data.addresses[e.currentTarget.dataset.idx].status });
      let aa = this.data.addresses;
      aa.forEach((element, index) => {
        if (index != e.currentTarget.dataset.idx) {
          element.status = false;
        } else {
          this.setData({ address: element.address })
        }
      });
      this.setData({ addresses: aa })
    },
    radiobtn1: function (e) {
      let arr = this.data.sourcedata[this.data.num].title;
      if(arr.includes(e.currentTarget.dataset.one)){
        arr.splice(arr.indexOf(e.currentTarget.dataset.one),1);
        this.setData({ ['sourcedata[' + this.data.num + '].title']: arr });
      }else{
        arr.push(e.currentTarget.dataset.one);
        this.setData({ ['sourcedata[' + this.data.num + '].title']: arr });
      }
      this.setData({ ['sourcedata[' + this.data.num + '].child[' + e.currentTarget.dataset.index + '].status']: !this.data.sourcedata[this.data.num].child[e.currentTarget.dataset.index].status })
      this.setData({ ['sourcedata1[' + this.data.num + '].child[' + e.currentTarget.dataset.index + '].status']: !this.data.sourcedata1[this.data.num].child[e.currentTarget.dataset.index].status })
    }
  }
})